class Sptitle < ActiveRecord::Base
  paginates_per 25
  mount_uploader :picture, PhotoUploader

  has_many :news_sptitles
  has_many :news,:through => :news_sptitles

  validates_presence_of :name



  def self.get_top_sptitles count
    result_array = Array.new()
    sptitles = Sptitle.where(over: 0).order("orders desc")
    sptitles.each do |sptitle|
      if news_count(sptitle.id) != 0
        result_array.push sptitle 
      elsif result_array.size == count
        break
      end
    end    
    return result_array[0..(count-1)]
  end

  private
  def self.news_count sptitle_id
    NewsSptitle.where(sptitle_id: sptitle_id).count
  end
end